package member.base.service;

import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.common.service.Service;
import m.system.db.DBManager;
import m.system.exception.MException;
import m.system.util.ArrayUtil;
import m.system.util.JSONMessage;
import m.system.util.SystemUtil;
import member.base.model.MenuInfo;
import member.base.model.UserInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuInfoService extends Service {
    private final static Map<String,List<JSONMessage>> menusMap=new HashMap<>();

    public List<JSONMessage> getMenus(UserInfo user) throws Exception {
        String[] powers=null!=user?user.getPowers():new String[]{UserInfo.PowerStore,UserInfo.PowerManage};
        String key= "power:"+ArrayUtil.connection(powers,",");
        if(null==menusMap.get(key)) {
            List<MenuInfo> ls = ModelQueryList.getModelList(MenuInfo.class, new String[]{"*"}, null,
                QueryCondition.or(
                    QueryCondition.eq("isPublic", MenuInfo.PublicMenu),
                    QueryCondition.in("power", powers)
                ),
                QueryOrder.asc("sort")
            );
            Map<String, Integer> idxMap = new HashMap<>();
            List<JSONMessage> menus = new ArrayList<>();
            for (MenuInfo menu : ls) {
                Integer idx = idxMap.get(menu.getGroupName());
                if (null == idx) {
                    menus.add(new JSONMessage()
                        .push("group", menu.getGroupName())
                        .push("menus", new ArrayList<MenuInfo>())
                    );
                    idx = menus.size() - 1;
                    idxMap.put(menu.getGroupName(), idx);
                }
                List<MenuInfo> ms = (List<MenuInfo>) menus.get(idx).get("menus");
                ms.add(menu);
            }
            menusMap.put(key,menus);
        }
        return menusMap.get(key);
    }

    public void reset(){
        try {
            DBManager.executeUpdate("DELETE FROM b_menu_info");
            insertMenus("我登记的",
                toPublicMenu("1001",1001,"登记订单","xinzengyanfaxuqiuquerenshenqing","登记新的订单","order/orderInfo.js"),
                toPublicMenu("1005",1005,"登记充值","gongzitiaoguanli","登记新的充值","order/memberRecharge.js"),
                toPublicMenu("1010",1010,"我的订单","wenjianzhengzhaoyinzhangwaijie","我登记的订单","order/myOrderInfoList.js"),
                toPublicMenu("1020",1020,"我的充值","feiyongyusuanshenqing","我登记的充值","order/myMemberRechargeList.js")
            );
            insertMenus("信息查询",
                toPublicMenu("2020",2020,"会员查询","kaoqinzu","会员信息查询","base/memberInfoList.js"),
                toPublicMenu("2030",2030,"员工查询","renshiguanli","员工信息查询","base/userInfoList.js"),
                toPublicMenu("2040",2040,"查询服务","jinhuodanguanli","查询服务商品","base/storeServeGoodsList.js"),
                toPublicMenu("2055",2055,"工资记录","gongzitiao","我的工资记录","order/wages/myMonthWagesList.js")
            );
            insertMenus("审核确认",
                toPrivateMenu("3010",3010,"订单确认","fahuodan","订单确认审核","order/checkOrderInfoList.js",UserInfo.PowerStore),
                toPrivateMenu("3020",3020,"充值确认","shouhoufeiyongshenqing","充值确认审核","order/checkMemberRechargeList.js",UserInfo.PowerStore),
                toPrivateMenu("3030",3030,"薪资计发","fukuanshenqing","月度薪资计发","order/wages/monthWagesList.js",UserInfo.PowerStore)
            );
            insertMenus("店铺管理",
                toPrivateMenu("6010",6010,"店铺订单","danganguanli","已完成的订单","order/storeOrderInfoList.js",UserInfo.PowerStore),
                toPrivateMenu("6020",6020,"店铺充值","jiesuanguanli","已完成的充值","order/storeMemberRechargeList.js",UserInfo.PowerStore),
                toPrivateMenu("6030",6030,"店铺工资","gongzitiao","店铺员工工资","order/wages/allMonthWagesList.js?type=store",UserInfo.PowerStore),
                toPrivateMenu("6035",6035,"店铺计划","yuanshijilu","店铺销售计划","order/storePlan.js",UserInfo.PowerStore),
                toPrivateMenu("6040",6040,"店铺结算","fukuanshenqing","店铺之间结算","order/settle/storeSettleList.js",UserInfo.PowerStore),
                toPrivateMenu("6051",6051,"店铺服务","jinhuodanguanli","店铺服务商品","base/storeServeGoodsList.js?edit=true",UserInfo.PowerStore)
            );
            insertMenus("商家管理",
                toPrivateMenu("8010",8010,"商家订单","wupinkucunguanli","商家全部订单","order/businessOrderInfoList.js",UserInfo.PowerManage),
                toPrivateMenu("8020",8020,"商家充值","jiesuanguanli","商家全部充值","order/businessMemberRechargeList.js",UserInfo.PowerManage),
                toPrivateMenu("8030",8030,"商家工资","gongzitiao","商家员工工资","order/wages/allMonthWagesList.js?type=business",UserInfo.PowerManage)
            );
            insertMenus("商家设置",
                toPrivateMenu("9010",9010,"商家设置","fenleiguanli","商家信息配置","base/businessInfo.js",UserInfo.PowerManage),
                toPrivateMenu("9011",9011,"店铺管理","xifenleixingpeizhi","店铺信息管理","base/storeInfoList.js",UserInfo.PowerManage),
                toPrivateMenu("9012",9012,"员工管理","renshiguanli","员工信息管理","base/userInfoList.js?edit=true",UserInfo.PowerManage),
                toPrivateMenu("9020",9020,"服务商品","jinhuodanguanli","服务商品管理","base/serveGoodsList.js?edit=true",UserInfo.PowerManage),
                toPrivateMenu("9050",9050,"会员级别","kehuguanli","配置会员级别","base/memberLevelList.js",UserInfo.PowerManage),
                toPrivateMenu("9051",9051,"升级配置","yongzhang","会员升级配置","base/levelUpSetList.js",UserInfo.PowerManage),
                toPrivateMenu("9052",9052,"会员管理","kaoqinzu","会员信息管理","base/memberInfoList.js",UserInfo.PowerManage)
            );
        } catch (Exception e) {
            SystemUtil.println("初始化菜单失败：",e.getMessage());
        }
    }
    private void insertMenus(String groupName, MenuInfo... menus) throws MException {
        for(MenuInfo menu : menus) menu.setGroupName(groupName);
        ModelUpdateUtil.insertModels(menus);
    }
    private MenuInfo toPublicMenu(String oid,int sort,String name,String icon,String description,String path) {
        return toMenu(oid,sort,name,icon,description,path,MenuInfo.PublicMenu,"");
    }
    private MenuInfo toPrivateMenu(String oid,int sort,String name,String icon,String description,String path,String power) {
        return toMenu(oid,sort,name,icon,description,path,MenuInfo.PrivateMenu,power);
    }
    private MenuInfo toMenu(String oid,int sort,String name,String icon,String description,String path,String isPublic,String power){
        MenuInfo menu=new MenuInfo();
        menu.setOid(oid);
        menu.setSort(sort);
        menu.setName(name);
        menu.setIcon(icon);
        menu.setDescription(description);
        menu.setPath(path);
        menu.setIsPublic(isPublic);
        menu.setPower(power);
        return menu;
    }
}
